<template>
	<view class="login">
		<view class="content">
			<!-- 头部logo -->
			<view class="header">
				<image src="../../static/img/logo.png"></image>
			</view>

			<!--  #ifdef  H5 || APP-PLUS -->
			<!-- 主体表单 -->
			<view class="main">
				<view class="main-list oBorder">
					<input class="main-input" v-model="phoneData" type="text" maxlength="11" placeholder="请输入用户名" />
				</view>
				<view class="main-list oBorder">
					<input class="main-input" v-model="passData" type="text" maxlength="32" placeholder="请输入密码" password="true" />
				</view>
			</view>
			<button :class="['buttonBorder',!isRotate?'dlbutton':'dlbutton_loading']" @tap="startLogin()">
				<view :class="isRotate?'rotate_loop':''">
					<text v-if="isRotate" class="cuIcon-loading1 "></text>
					<text v-if="!isRotate" >登 录</text>
				</view>
			</button>
			<!-- 底部信息 -->
			<view class="footer">
				<!-- <navigator url="forget" open-type="navigate">找回密码</navigator>
				<text>|</text>
				<navigator url="register" open-type="navigate">注册账号</navigator> -->
			</view>
			<!--  #endif -->

			<!--  #ifdef  MP-WEIXIN || APP-PLUS-->
			<view class="footer">
				<image @tap="wxlogin" style="width: 60upx;height: 60upx;" src="../../static/img/share/wx.png"></image>
				<button style="margin:0;font-size:16px;background:none;color:#000" open-type="getUserInfo" lang="zh_CN"
				 @getuserinfo="wxlogin" @tap="wxlogin">微信登录</button>
			</view>
			<!--  #endif -->
		</view>
	</view>
</template>

<script>
	var _this;

	export default {
		data() {
			return {
				//logo图片 base64
				phoneData: '', //用户/电话
				passData: '', //密码
				isRotate: false, //是否加载旋转
				options: {}
			};
		},
		mounted() {
			_this = this;
		},
		/**
		 * 生命周期函数--监听页面加载
		 */
		onLoad: function(options) {
			let _this = this;
			_this.setData({
				options
			});
		},
		onShow: function() {
			if (uni.getStorageSync('token')) {
				uni.switchTab({
					url: '/pages/user/index'
				});
			}
		},
		methods: {
			wxlogin: function() {
				let _this = this;

				uni.checkSession({
					complete: function(res) {
						console.log(res);
					}
				});
				uni.showLoading({
					title: "正在登录",
					mask: true
				});
				uni.getProvider({
					service: 'oauth',
					success: function(res) {
						//支持微信、qq和微博等
						if (~res.provider.indexOf('weixin')) {
							// 登录
							uni.login({
								provider: 'weixin',
								success: function(loginRes) {
									console.log('-------获取openid(unionid)-----');
									console.log(JSON.stringify(loginRes));
									// 获取用户信息
									uni.getUserInfo({
										provider: 'weixin',
										success: function(infoRes) {
											console.log('-------获取微信用户所有-----');
											console.log(JSON.stringify(infoRes.userInfo));
											// APP登录
											// #ifdef APP-PLUS
											_this.applogin(loginRes, infoRes);
											// #endif

											// 微信小程序登录
											// #ifdef  MP-WEIXIN
											_this.xcxlogin(loginRes, infoRes);
											// #endif
										},
										fail: function(res) {
											console.log(JSON.stringify(res));
										}
									});
								}
							});
						}
					}
				});

				return false;
			},
			applogin: function(loginRes, infoRes) {
				let _this = this;
				_this._post_form('user/login_uniapp?type=1', {
					user_info: JSON.stringify(infoRes.userInfo),
					referee_id: uni.getStorageSync('referee_id')
				}, function(result) {
					// console.log(result);return false;
					console.log(result.data.token);
					uni.setStorageSync('token', result.data.token);
					uni.setStorageSync('user_id', result.data.user_id);
					console.log('登陆成功');
					// 跳转回原页面/
					_this.navigateBack();
				}, false, function() {
					uni.hideLoading();
				});
			},

			xcxlogin: function(loginRes, infoRes) {
				let _this = this;
				_this._post_form('user/login_uniapp?type=2&code=' + loginRes.code, {
					code: loginRes.code,
					user_info: JSON.stringify(infoRes.userInfo),
					encrypted_data: JSON.stringify(infoRes.encryptedData),
					iv: JSON.stringify(infoRes.iv),
					signature: JSON.stringify(infoRes.signature),
					referee_id: uni.getStorageSync('referee_id')
				}, function(result) {
					console.log(result.data.token);
					// return false;
					// 记录token user_id
					console.log(result.data.token);
					uni.setStorageSync('token', result.data.token);
					uni.setStorageSync('user_id', result.data.user_id);
					console.log('登陆成功');
					// 跳转回原页面/
					_this.getUserDetail();
				}, false, function() {
					uni.hideLoading();
				});
			},
			
			/**
			 * 获取当前用户信息
			 */
			getUserDetail: function() {
				let _this = this;
				_this._get('user.index/detail', {}, function(result) {
					uni.setStorageSync('nickName', result.data.userInfo.nickName);
					uni.setStorageSync('avatarUrl', result.data.userInfo.avatarUrl);
					uni.hideLoading();
					// 跳转回原页面/
					_this.navigateBack();
				});
			},
			/**
			 * 授权成功 跳转回原页面
			 */
			navigateBack: function() {
				uni.switchTab({
					url: '/pages/index/index'
				});
			},
			startLogin() {
				//登录
				if (this.isRotate) {
					//判断是否加载中，避免重复点击请求
					return false;
				}
				if (this.phoneData.length == "") {
					uni.showToast({
						icon: 'none',
						position: 'bottom',
						title: '用户名不能为空'
					});
					return;
				}
				if (this.passData.length == "") {
					uni.showToast({
						icon: 'none',
						position: 'bottom',
						title: '密码不能为空'
					});
					return;
				}

				_this.isRotate = true

				_this._post_form('user/userlogin', {
					phoneData: _this.phoneData, // 用户/电话
					passData: _this.passData, //密码
				}, function(result) {
					if (result.data.state === 1) {
						_this.showError(result.data.text);
					}

					if (result.data.state === 0) {
						console.log(result.data.token);
						uni.setStorageSync('token', result.data.token);
						uni.setStorageSync('user_id', result.data.user_id);
						console.log('登陆成功');
						// 跳转回原页面/
						_this.navigateBack();
					}

					_this.isRotate = false
					return false;
				}, false, function() {
					uni.hideLoading();
				});

				setTimeout(function() {
					_this.isRotate = false
				}, 3000)


			}
		}
	}
</script>

<style>
	@import url("./css/main.css");
</style>
